<?php

namespace App\Console\Commands\OpenShop\Ekb;

use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseOrderProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Service\Cruise\CruiseTemplate\CruiseTemplateService;
use App\Service\Cruise\Mission\CruiseOrderInputService;
use Exception;
use Illuminate\Console\Command;

class ResetCruiseTemplateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ekb:resetCruiseTemplateCommand {orderNo}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '易快报：重置帮扶订单模板信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws Exception
     */
    public function handle(): int
    {
        $orderNo                  = $this->input->getArgument("orderNo");
        $openShopMainProcessModel = OpenShopMainProcessModel::query()->where("orderNo", $orderNo)->first();
        if (empty($openShopMainProcessModel)) {
            throw new Exception("获取主订单信息失败");
        }
        $templateId       = CruiseTemplateService::getTemplateId(CruiseOrderModel::TYPE_NEW_SHOP_HELP);
        $cruiseOrderModel = (new CruiseOrderModel)->initShopHelper(
            $orderNo,
            $openShopMainProcessModel->shopId,
            $templateId
        );
        CruiseOrderProcessModel::initCruiseOrderProcess($cruiseOrderModel->id);
        (new CruiseOrderInputService($cruiseOrderModel))->initOrderInput($templateId);
        return 0;
    }
}
